Dashboard Temp Share Shortlinks Frames API

HTMLify

LRU cache.java
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Node{
    Node next;
    Node prev;
    int key;
    int val;
    public Node(){

    }
}

class LRUCache {
    HashMap<Integer,Node> hm;
    Node H=new Node();
    Node T=new Node();
    int cap;
    public LRUCache(int capacity) {
        hm=new HashMap<>();
        H.next=T;
        T.prev=H;
        cap=capacity;

    }

    public Node delete(Node h, Node del){
        Node dp1=del.next;
        Node dm1=del.prev;
        dp1.prev=dm1;
        dm1.next=dp1;
        del.prev=null;
        del.next=null;
        return del;
    }
    
    public void add(Node h, Node t, Node n){
        Node tm1=t.prev;
        tm1.next=n;
        n.prev=tm1;
        n.next=t;
        t.prev=n;
    }

    public int get(int key) {
        if(hm.get(key)==null){
            return -1;
        }else{
            int ans=hm.get(key).val;
            Node temp=delete(H,hm.get(key));
            add(H,T,temp);
            return ans;
        }
    }
    
    public void put(int key, int value) {
        Node temp=hm.get(key);

        if(temp==null){
            if(hm.size()==cap){
                hm.remove(H.next.key);
                delete(H,H.next);
            }
            Node n=new Node();
            n.key=key;
            n.val=value;
            hm.put(key,n);
            add(H,T,n);
        }else{
            Node t=delete(H,temp);
            t.val=value;
            add(H,T,temp);
        }
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */